package display;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import universe.Game;

@SuppressWarnings("serial")
public class GameDataDisplay extends JPanel implements Observer{
	
	
	private Game		 theGame;
	private Container	 theContainerDataGame;
	//private BlockDisplay theBlockDisplay;
	private NextBlockDisplay theNextBlockDisplay;
	private JPanel 		 theBackgroundPanel;
	private JLabel		 theScore; 
	private JLabel		 theLinesNumber;
	private JLabel		 theLevel;
	
	public GameDataDisplay(Container pContainerDataGame, Game pGame){
		super();
		this.setSize(Utils.gameDataDisplayWidth, Utils.gameDataDisplayHeight);
		//this.setBackground(Color.MAGENTA);
		this.setVisible(true);
		
		
		//System.out.println("constructeur de GameDataDisplay");
		this.setGame(pGame);
		this.setContainerDataGame(pContainerDataGame);
		this.theNextBlockDisplay = new NextBlockDisplay(this.getContainerDataGame(), this.getGame().getNextBlock());
		//this.theBlockDisplay = new BlockDisplay(this.getContainerDataGame(), this.getGame().getNextBlockHandler(),(((Utils.gameDataDisplayWidth / Utils.sideWidth) / 2) -  2) ,1);
		
		this.theBackgroundPanel = new JPanel();
		this.theBackgroundPanel.setSize(Utils.gameDataDisplayWidth, Utils.gameDataDisplayHeight);
		
		//this.getContainerDataGame().add(this.theBackgroundPanel);
		//this.initializeBackgroundPanel();
		
		this.theScore = new JLabel("",SwingConstants.CENTER);
		this.theLinesNumber = new JLabel("",SwingConstants.CENTER);
		this.theLevel = new JLabel("",SwingConstants.CENTER);
		this.initializeScore();
		this.getContainerDataGame().add(this.theScore);
		this.getContainerDataGame().add(this.theLinesNumber);
		this.getContainerDataGame().add(this.theLevel);
		
		theContainerDataGame.add(this);
		
		
	}
	
	
	public void update(Observable o, Object arg) {
		// Ceci est un gros test de ouf mais il le faut en fait !
		//this.theBlockDisplay.setBlockHandler(this.getGame().getNextBlockHandler());
		this.theNextBlockDisplay.setBlock(this.getGame().getNextBlock());
		this.theScore.setText("Score : "+String.valueOf(theGame.getScore()));
		this.theLinesNumber.setText("Lignes : "+String.valueOf(theGame.getNblignes()));
		this.theLevel.setText("Niveau : "+String.valueOf(theGame.getNiveau()));
		this.getContainerDataGame().repaint();
	}
	

	public void setGame(Game pGame) {
		this.theGame = pGame;
		pGame.addObserver(this);
	}

	public Game getGame() {
		return theGame;
	}

	public void setContainerDataGame(Container pContainerDataGame) {
		this.theContainerDataGame = pContainerDataGame;
	}

	public Container getContainerDataGame() {
		return theContainerDataGame;
	}
	
	public void initializeScore(){
		this.theScore.setBounds(0, (2*Utils.paddingFrame)+Utils.getNextBlockDisplayHeightPx, Utils.gameDataDisplayWidth,20);
		this.theScore.setAlignmentY(CENTER_ALIGNMENT);
		this.theScore.setFont(new Font("ARIAL", 0, 15));
		this.theScore.setForeground(Utils.gameDataDisplayFontColor);
		this.theLinesNumber.setBounds(0, (2*Utils.paddingFrame+25)+Utils.getNextBlockDisplayHeightPx, Utils.gameDataDisplayWidth,20);
		this.theLinesNumber.setAlignmentY(CENTER_ALIGNMENT);
		this.theLinesNumber.setFont(new Font("ARIAL", 0, 15));
		this.theLinesNumber.setForeground(Utils.gameDataDisplayFontColor);
		this.theLevel.setBounds(0, (2*Utils.paddingFrame+50)+Utils.getNextBlockDisplayHeightPx, Utils.gameDataDisplayWidth,20);
		this.theLevel.setAlignmentY(CENTER_ALIGNMENT);
		this.theLevel.setFont(new Font("ARIAL", 0, 15));
		this.theLevel.setForeground(Utils.gameDataDisplayFontColor);
	}
	
	
	public void paintComponent(Graphics g){
		/*
		 Affichage de la prochaine pièce géré par BlockDisplay
		g.setColor(Color.WHITE);
		g.fillRect(PADDING_PANEL, 0, Utils.gameDataDisplayWidth-PADDING_PANEL, (Utils.getBlockHeightPx() * 3) /2);
		
		g.setColor(Color.BLACK);
		g.drawRect(PADDING_PANEL, 0, Utils.gameDataDisplayWidth-PADDING_PANEL-1, (Utils.getBlockHeightPx() * 3) /2);
		*/
		Color theColor = new Color(Utils.gameDataDisplayColor.getRed(), Utils.gameDataDisplayColor.getGreen(), Utils.gameDataDisplayColor.getBlue(), display.Utils.gameDataDisplayAlpha);
		g.setColor(theColor);
		g.fill3DRect(Utils.paddingFrame, (2*Utils.paddingFrame)+Utils.getNextBlockDisplayHeightPx-20, Utils.gameDataDisplayWidth/2, 110, true);
	}

}
